import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-08-17
 * Time: 17:43
 */

/**
 * 代码中的 -i操作 & 布尔变量 的设置均为优化，普通的冒泡排序没有该优化
 */
public class Test {
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < arr.length-1-i; j++) {
                if (arr[j] > arr[j+1]) {
                    swap(arr,j,j+1);
                    flg = true;
                }
            }
            if (!flg) break;
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    public static void main(String[] args) {
        int[] arr = {23,122,0,12,5657,12,-14,2,7,2,19,24,26,10,88,66,16};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
}